home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / TEXT_MEN.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  7.2 KB  |  282 lines

  1. package sub_arctic.lib;
  2. import sub_arctic.input.*;
  3. import sub_arctic.output.*;
  4.  
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Color;
  8. import java.awt.Dimension;
  9.  
  10. /**
  11.  * This class implements a menu_item with a string of text on it. 
  12.  *
  13.  * @see menu
  14.  * @author Ian Smith
  15.  */
  16. public class text_menu_item extends menu_item {
  17.   
  18.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  19.  
  20.   /**
  21.    * The text string on this object.
  22.    */
  23.   protected String _text;
  24.  
  25.   /** 
  26.    * Return the text on this object. 
  27.    * @return String the string of text on this object.
  28.    */
  29.   public String text() { return _text;};
  30.  
  31.   /**
  32.    * Set the string on this object. This function does NOT
  33.    * imply a re-computation of the width of this object.
  34.    *
  35.    * @param String s the new text string for this object.
  36.    */
  37.   public void set_text(String s) { 
  38.     _text=s;
  39.     style_changed();
  40.   }
  41.  
  42.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  43.  
  44.   /**
  45.    * This objects font. It will be null if they want the system
  46.    * default font.
  47.    */
  48.   protected Font _font=null;
  49.  
  50.   /**
  51.    * Return this object's font. This will return the system's default
  52.    * font if you don't set the font explicitly. 
  53.    * @return Font the font in use for this object.
  54.    */
  55.   public Font font() { 
  56.     if (_font==null) {
  57.       return style_manager.default_font();
  58.     } else {
  59.       return _font;
  60.     }
  61.   }
  62.  
  63.   /**
  64.    * Set the font used for this object. Set this to null if you want
  65.    * the system default font. 
  66.    * @param Font f the font for this object.
  67.    */
  68.   public void set_font(Font f) { 
  69.     _font=f;
  70.     style_changed();
  71.   }
  72.  
  73.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  74.  
  75.   /**
  76.    * The amount of border to use in x.
  77.    */
  78.   protected int _x_border = 2;
  79.  
  80.   /**
  81.    * Return the size of the x border.
  82.    * @return int the number of pixels of horizontal spacing (appears on both 
  83.    *             right and left).
  84.    */
  85.   public int x_border() { return _x_border;}
  86.  
  87.   /**
  88.    * Change the amount of horizontal spacing
  89.    * @param int v the new amount of x spacing.
  90.    */
  91.   public void set_x_border(int v) { 
  92.     _x_border=v;
  93.     style_changed();
  94.   }
  95.  
  96.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  97.  
  98.   /**
  99.    * The amount of border to use in y.
  100.    */
  101.   protected int _y_border=1;
  102.  
  103.   /**
  104.    * Return the size of the y border.
  105.    * @return int the number of pixels of vertical spacing (appears on both 
  106.    *             top and bottom).
  107.    */
  108.   public int y_border() { return _y_border;}
  109.  
  110.   /**
  111.    * Change the amount of vertical spacing.
  112.    * @param int v the new amount of y spacing.
  113.    */
  114.   public void set_y_border(int v) { 
  115.     _y_border=v;
  116.     style_changed();
  117.   }
  118.  
  119.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  120.  
  121.   /**
  122.    * Store the two looks for this object.
  123.    */
  124.   protected loaded_image[] _look;
  125.  
  126.   /*
  127.    * Compute the new images for this object.
  128.    */
  129.   protected void style_changed() {
  130.     style cs=style_manager.current_style();
  131.  
  132.     _look=cs.menu_item_image(text(),font(),x_border(),y_border(),
  133.                 w(),h(),false);
  134.     damage_self();
  135.   }
  136.  
  137.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  138.  
  139.   /**
  140.    * Draw yourself... depends the value of the highlight func.
  141.    * defined in menu_item.
  142.    * @param drawable d the surface to draw yourself on.
  143.    */
  144.   protected void draw_self_local(drawable d) {
  145.     FontMetrics metrics=manager.get_metrics(font());
  146.     Color fg,bg;
  147.  
  148.     /* if highlighted draw down, otherwise draw up */
  149.     if (highlighted()) {
  150.       d.drawImage(_look[1],0,0);
  151.     } else {
  152.       d.drawImage(_look[0],0,0);
  153.     }
  154.   }
  155.  
  156.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  157.  
  158.   /**
  159.    * Return that our width and height are intrinsically computed. 
  160.    * @return int the constant value of W | H
  161.    */
  162.   public int intrinsic_constraints() {
  163.     return (W | H);
  164.   }
  165.  
  166.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  167.  
  168.   /** 
  169.    * Construct a text menu item given a string, a font, and a width.
  170.    * @param String s     the string to be displayed on the item.
  171.    * @param Font   f     the font to use for this item.
  172.    * @param int    width the width of this object. 
  173.    */
  174.   public text_menu_item(String s, Font f, int width) 
  175. {
  176.     super();
  177.  
  178.     Dimension d;
  179.     style cs=style_manager.current_style();
  180.  
  181.     _font=f;
  182.     _text=s;
  183.     d=cs.menu_item_natural_size(s,font(),2,1);
  184.     set_intrinsic_w(width);
  185.     set_intrinsic_h(d.height);
  186.     style_changed();
  187.   }
  188.  
  189.    //had:
  190.    //* @exception general PROPAGATED.
  191.  
  192.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  193.  
  194.   /**
  195.    * Simple default version of a constructor which doesn't require
  196.    * a font.
  197.    * @param String s     the string to be displayed on the item.
  198.    * @param int    width the width of this object.
  199.    */
  200.   public text_menu_item(String s,int width) 
  201. {
  202.     super();
  203.  
  204.     style cs=style_manager.current_style();
  205.     Dimension d=cs.menu_item_natural_size(s,font(),2,1);
  206.  
  207.     _text=s;
  208.     set_intrinsic_w(width);
  209.     set_intrinsic_h(d.height);
  210.     style_changed();
  211.   }
  212.  
  213.    //had:
  214.    //* @exception general PROPAGATED
  215.  
  216.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  217.  
  218.   /**
  219.    * Return a debugging string.
  220.    * @return String a string representing this object and its position.
  221.    */
  222.   public String toString() { 
  223.     return "text_menu_item: [" + x() + "," + y() + "]";
  224.   }
  225.  
  226.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  227.  
  228.   /**
  229.    * This function is a convenience for figuring out which of a set
  230.    * of strings will have the widest menu and item and what the
  231.    * widest value is.  It assumes the default spacing of 2 in x and 
  232.    * 1 in y.
  233.    * 
  234.    * @param String[] strings the array of strings to consider.
  235.    * @param Font     f       the font you'll be using to render these 
  236.    *                         strings (use null for a default font).
  237.    * @return int the width of the widest of these strings
  238.    */
  239.   public static int max_item_width(String[] strings, Font f) {
  240.     Font font;
  241.     int max=-1,i;
  242.     Dimension d;
  243.     style cs=style_manager.current_style();
  244.  
  245.     /* get the font right */
  246.     if (f==null) {
  247.       font=style_manager.default_font();
  248.     } else {
  249.       font=f;
  250.     }
  251.  
  252.     /* loop over all the strings */
  253.     for (i=0; i<strings.length; ++i) {
  254.       d=cs.menu_item_natural_size(strings[i],font,2,1);
  255.       /* if its bigger, make it the max */
  256.       if (d.width>max) max=d.width;
  257.     }
  258.  
  259.     /* tell the user how big it was */
  260.     return max;
  261.   }
  262.  
  263.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  264.  
  265. }
  266. /*=========================== COPYRIGHT NOTICE ===========================
  267.  
  268. This file is part of the subArctic user interface toolkit.
  269.  
  270. Copyright (c) 1996 Scott Hudson and Ian Smith
  271. All rights reserved.
  272.  
  273. The subArctic system is freely available for most uses under the terms
  274. and conditions described in 
  275.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  276. and appearing in full in the lib/interactor.java source file.
  277.  
  278. The current release and additional information about this software can be 
  279. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  280.  
  281. ========================================================================*/
  282.